﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using KPIS.DBM;
using KPIS.GERP.SERVICE.Model;

namespace KPIS.GERP.SERVICE.DAL
{
    public class User
    {
        Employee employee = new Employee();
        Common common = new Common();
        DBManager dbManager = new DBManager(DataProvider.MySQL, ConfigurationSettings.AppSettings["ConnectionString"]);

        #region Select
        public List<sys_sc_users> GetUser(string user_seq, string emp_seq, string user_code, string user_password, bool isGetEmployee)
        {
            DataTable dtb;
            List<sys_sc_users> list = new List<sys_sc_users>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(5);
                dbManager.AddParameters(0, "@delete_status", Common.DeletedStatus);
                dbManager.AddParameters(1, "@user_seq", user_seq);
                dbManager.AddParameters(2, "@emp_seq", emp_seq);
                dbManager.AddParameters(3, "@user_code", user_code);
                dbManager.AddParameters(4, "@user_password", user_password);

                string strSQL = "select * from sys_sc_users where record_status <> @delete_status"
                    + (!string.IsNullOrWhiteSpace(user_seq) ? " and user_seq = @user_seq" : "")
                    + (!string.IsNullOrWhiteSpace(emp_seq) ? " and emp_seq = @emp_seq" : "")
                    + (!string.IsNullOrWhiteSpace(user_code) ? " and user_code = @user_code" : "")
                    + (!string.IsNullOrWhiteSpace(user_password) ? " and user_password = @user_password" : "")
                    ;

                dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];
                list = (List<sys_sc_users>)CollectionHelper.ConvertTo<sys_sc_users>(dtb);

                foreach (sys_sc_users info in list)
                {
                    if (isGetEmployee && info.emp_seq != null)
                    {
                        List<hrm_ps_employees> sub_list = employee.GetHrmPsEmployee(info.emp_seq.ToString()
                            , null, null, null, null, null, null, null, true, false, false, false, false, false
                            , true, false, true, true, false, false, true, false);

                        info.hrm_ps_employees = sub_list.Count > 0 ? sub_list[0] : null;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (dbManager.Connection != null) {  dbManager.Connection.Close(); dbManager.Dispose();  }
            }

            return list;
        }
        #endregion
    }
}
